package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

// 关联攻略标签
@Controller
@RequestMapping("/strategyTag")
public class StrategyTagController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITagService tagService;

    // 大家都在看攻略标签
    @RequestMapping("list")
    public String list(Model model, Long strategyId) {
        // 查询所有攻略
        model.addAttribute("strategy", strategyService.selectAll());
        model.addAttribute("strategyId", strategyId);
        // 查询所有标签
        List<Tag> tags = tagService.listAll();
        model.addAttribute("tag", tagService.listAll());
        // 根据strategyId查询对应的标签
        List<Tag> list = tagService.queryTagForStrategyByStrategyId(strategyId);
        model.addAttribute("list", list);
        return "strategyTag/list";
    }

    //保存关联标签
    @RequestMapping("/save")
    @ResponseBody
    public Object insertRelation(Long strategyId, Long tagId) {
        JSONResult result = new JSONResult();
        try {
            tagService.insertRelation(strategyId, tagId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    // 删除关联标签
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteRelation(Long strategyId, Long tagId) {
        tagService.deleteRelation(strategyId, tagId);
        return "redirect:/strategyTag/list";
    }
}
